package com.nuc.wxh.vaccine_appointment.vaccines.controller.system;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuc.wxh.vaccine_appointment.base.result.R;
import com.nuc.wxh.vaccine_appointment.vaccines.entity.AdminHospital;
import com.nuc.wxh.vaccine_appointment.vaccines.entity.HospitalVaccine;
import com.nuc.wxh.vaccine_appointment.vaccines.entity.SysVaccinator;
import com.nuc.wxh.vaccine_appointment.vaccines.entity.VaccineAppointment;
import com.nuc.wxh.vaccine_appointment.vaccines.entity.vo.HospitalVaccineVo;
import com.nuc.wxh.vaccine_appointment.vaccines.service.HospitalVaccineService;
import com.nuc.wxh.vaccine_appointment.vaccines.service.VaccineAppointmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 疫苗预约详细情况 前端控制器
 * </p>
 *
 * @author 王鑫浩
 * @since 2021-03-10
 */
@Api(tags = "预约相关")
@CrossOrigin
@RestController
@RequestMapping("/user/vaccine-appointment")
public class VaccineAppointmentController {

    @Autowired
    VaccineAppointmentService vaccineAppointmentService;

    @Autowired
    private HospitalVaccineService hospitalVaccineService;

    @Transactional
    @ApiOperation("新增预约")
    @PostMapping("save")
    public R save(@ApiParam(value = "预约信息对象", required = true) @RequestBody VaccineAppointment vaccineAppointment) {
        vaccineAppointment.setIsState(1);
        boolean result = vaccineAppointmentService.save(vaccineAppointment);
        if (result) {
            QueryWrapper<HospitalVaccine> wrapper = new QueryWrapper<>();
            wrapper.eq("hospital_id", vaccineAppointment.getHospitalId());
            wrapper.eq("vaccine_id", vaccineAppointment.getVaccineId());
            List<HospitalVaccine> list = hospitalVaccineService.list(wrapper);
            HospitalVaccine hospitalVaccine = list.get(0);
            HospitalVaccine hospitalvaccine2 = hospitalVaccineService.getById(hospitalVaccine.getId());
            int num = hospitalvaccine2.getVaccineNumber();
            hospitalvaccine2.setVaccineNumber(--num);
            System.out.println(hospitalVaccine.getVaccineNumber());
            hospitalVaccineService.updateById(hospitalvaccine2);
            return R.ok().message("添加成功");
        } else {
            return R.error().message("添加失败");
        }
    }

    @ApiOperation("查询进行中预约信息")
    @GetMapping("list/{id}")
    public R list(@ApiParam(value = "查询对象条件", required = true) @PathVariable String id) {
        QueryWrapper<VaccineAppointment> wrapper = new QueryWrapper<>();
        wrapper.eq("sys_user_id", id);
        wrapper.notIn("is_state", Arrays.asList(0, 3, 4,-1));
        List<VaccineAppointment> list = vaccineAppointmentService.list(wrapper);
        return R.ok().data("rows", list);
    }

    @ApiOperation("查询已完成预约信息")
    @GetMapping("listfinish/{id}")
    public R listfinish(@ApiParam(value = "查询对象条件", required = true) @PathVariable String id) {
        QueryWrapper<VaccineAppointment> wrapper = new QueryWrapper<>();
        wrapper.eq("sys_user_id", id);
        wrapper.notIn("is_state", Arrays.asList(1, 2));
        List<VaccineAppointment> list = vaccineAppointmentService.list(wrapper);
        return R.ok().data("rows", list);
    }


    @ApiOperation("删除记录")
    @DeleteMapping("remove/{id}")
    public R updateById(@ApiParam(value="预约id",required=true)@PathVariable String id){
        boolean result = vaccineAppointmentService.removeById(id);
        if (result) {
            return R.ok().message("删除成功");
        } else {
            return R.error().message("删除失败");
        }
    }

    @Transactional
    @ApiOperation("取消预约")
    @PutMapping("cancel/{id}")
    public R cancel(@ApiParam(value = "查询对象条件", required = true) @PathVariable String id) {
        VaccineAppointment vaccineAppointment=vaccineAppointmentService.getById(id);
        vaccineAppointment.setIsState(0);
        vaccineAppointmentService.updateById(vaccineAppointment);

        QueryWrapper<HospitalVaccine> wrapper = new QueryWrapper<>();
        wrapper.eq("hospital_id", vaccineAppointment.getHospitalId());
        wrapper.eq("vaccine_id", vaccineAppointment.getVaccineId());
        List<HospitalVaccine> list = hospitalVaccineService.list(wrapper);
        HospitalVaccine hospitalVaccine = list.get(0);
        HospitalVaccine hospitalvaccine2 = hospitalVaccineService.getById(hospitalVaccine.getId());
        int num = hospitalvaccine2.getVaccineNumber();
        hospitalvaccine2.setVaccineNumber(++num);
        System.out.println(hospitalVaccine.getVaccineNumber());
        hospitalVaccineService.updateById(hospitalvaccine2);
        return R.ok();
    }



    @ApiOperation("确认完成接种")
    @PutMapping("complete/{id}")
    public R complete(@ApiParam(value = "查询对象条件", required = true) @PathVariable String id) {
        VaccineAppointment vaccineAppointment=vaccineAppointmentService.getById(id);
        vaccineAppointment.setIsState(3);
        vaccineAppointmentService.updateById(vaccineAppointment);
        return R.ok();
    }


    @ApiOperation("反馈")
    @PutMapping("back")
    public R complete(@ApiParam(value = "反馈信息", required = true) @RequestBody VaccineAppointment vaccineAppointment) {
        vaccineAppointment.setIsState(4);
        vaccineAppointmentService.updateById(vaccineAppointment);
        return R.ok();
    }
}
